package delegate4j;

import com.sun.istack.internal.Nullable;

import java.util.concurrent.FutureTask;

public interface IFunc<TResult> extends IDelegate {

    TResult invoke() throws Exception;

    default FutureTask<TResult> invokeAsync(@Nullable Callback callback) {
        FutureTask<TResult> futureTask = new FutureTask<>(() -> {
            TResult result = invoke();
            if (callback != null) {
                callback.invoke(new InvokeData(this, null, result));
            }
            return result;
        });

        ASYNC_DELEGATE_THREAD_POOL.submit(futureTask);
        return futureTask;
    }
}
